package com.herench.bs.handler;

import com.herench.bs.transfer.Dialect;
import com.herench.bs.agent.IOutpatientClinicAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;

/**
 * com.herench.bs.handler
 *
 * @author wangwenkai
 * @date 2017/10/23/15:04
 * E-mail：605064334@qq.com
 */
@Component
@Handler(code = "100711" ,name="当日挂号数据保存")
public class SaveRegisterHandler extends AbstractMessageHandler {

    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = new ResponseMessage(requestMessage.getId());
        String channelCode = requestMessage.getArgsPropertyStringValue("sysCode");
        String doctorCode = requestMessage.getArgsPropertyStringValue("docId");
        String doctorName = requestMessage.getArgsPropertyStringValue("docName");
        Assert.notNull(doctorName, "docName 不可为空！");
        String deptCode = requestMessage.getArgsPropertyStringValue("deptId");
        Assert.notNull(deptCode, "deptId 不可为空！");
        String user = requestMessage.getArgsPropertyStringValue("user");
        Assert.notNull(user, "user 不可为空！");
        String phone = requestMessage.getArgsPropertyStringValue("phone");
        Assert.notNull(phone, "phone 不可为空！");
        String idCard = requestMessage.getArgsPropertyStringValue("idCard");
        Assert.notNull(idCard, "idCard 不可为空！");
        String name = requestMessage.getArgsPropertyStringValue("name");
        Assert.notNull(name, "name 不可为空！");
        String cardId = requestMessage.getArgsPropertyStringValue("cardId");
        Assert.notNull(cardId, "cardId 不可为空！");
        String cardType = requestMessage.getArgsPropertyStringValue("cardType");
        Assert.notNull(cardType, "cardType 不可为空！");
        String schDate = requestMessage.getArgsPropertyStringValue("schDate");
        Assert.notNull(schDate, "schDate 不可为空！");
        String period = requestMessage.getArgsPropertyStringValue("ampm");
        Assert.notNull(period, "ampm 不可为空！");
        period = Dialect.getAmpmValue(period);
        String clinicType = requestMessage.getArgsPropertyStringValue("categor");
        Assert.notNull(clinicType, "categor 不可为空！");
        String regFee = requestMessage.getArgsPropertyStringValue("regFee");
        Assert.notNull(regFee, "regFee 不可为空！");
        String chargeType = requestMessage.getArgsPropertyStringValue("chargeType");
        Assert.notNull(chargeType, "chargeType 不可为空！");
        String source = requestMessage.getArgsPropertyStringValue("source");
        Assert.notNull(source, "source 不可为空！");
        String clinicLabel = requestMessage.getArgsPropertyStringValue("clinicLabel");
        Assert.notNull(clinicLabel, "clinicLabel 不可为空！");
        Map<String, Object> res = new HashMap<>(16);
        IOutpatientClinicAgent outpatientClinicAgent = getCleverClinicContext().getApplicationContext().getBean(IOutpatientClinicAgent.class);
        String hospitalCode = getCleverClinicContext().getHospitalCodeByChannelCode(channelCode);
        Map<String,Object> resUnpaidOrders = outpatientClinicAgent.loadUnpaidOrders(channelCode,hospitalCode,idCard,source,user,"3");
        if (Dialect.getSuccessfulState().equals(resUnpaidOrders.get("code"))) {
            res.put("dataList",resUnpaidOrders.get("dataList"));
            res.put(ResponseMessage.RES_CODE, "-001");
            res.put(ResponseMessage.MESSAGE, "存在未支付订单");
            res.put(ResponseMessage.MESSAGE_IN, "存在未支付订单");
            res.put(ResponseMessage.MESSAGE_OUT, "存在未支付订单");
        }else {
            Map<String, Object> resPatientInfo = outpatientClinicAgent.extractCreationPatient(channelCode, hospitalCode, idCard);
            String patientId = (String) resPatientInfo.get("patientId");
            String age = (String) resPatientInfo.get("age");
            String gender = (String) resPatientInfo.get("sex");
            if (!(patientId == null || "".equals(patientId))) {
                res = outpatientClinicAgent.saveRegister(channelCode, hospitalCode, name, gender, age, clinicLabel, regFee, period, user, idCard, patientId, chargeType, clinicType, deptCode, doctorCode, doctorName, cardId, cardType);
            } else {
                res = resPatientInfo;
            }
        }

        responseMessage.setData(res);
        getPropertiesFromInvokerResultMap(responseMessage, res);
        return responseMessage;
    }
}
